package com.his.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.his.entity.Diagnosis;
import com.his.entity.ResponseResult;
import com.his.entity.Staff;
import com.his.entity.User;
import com.his.mapper.StaffMapper;
import com.his.service.IStaffService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 员工表 服务实现类
 * </p>
 *
 * @author Demoon Liu
 * @since 2022-02-20
 */
@Service
public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff> implements IStaffService {



    @Resource
    public StaffMapper staffMapper;

    @Override
    public ResponseResult addStaff(Staff staff) {
        int code;
        String message;


        staff.setCreatetime(LocalDateTime.now());


        if(staffMapper.insert(staff)>0) {
            message = "插入成功";
            code = 201;
        }
        else {
            message = "插入失败";
            code = 400;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("staff", staff);



        return new ResponseResult(code, message,map);
    }

    @Override
    public ResponseResult deleteStaff(long id) {
        int code;
        String message;

        if(staffMapper.deleteById(id)>0) {
            message = "删除成功";
            code = 201;
        }
        else {
            message = "删除失败";
            code = 400;
        }


        return new ResponseResult(code, message,"");

    }

    @Override
    public ResponseResult updateStaff(Staff staff) {
        int code;
        String message;

        if(staffMapper.updateById(staff)>0) {
            message = "修改成功";
            code = 201;
        }
        else {
            message = "修改失败";
            code = 400;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("staff", staff);

        return new ResponseResult(code, message,map);
    }

    @Override
    public ResponseResult selectStaffByUsername(Staff staff) {
        int code;
        String message;

        QueryWrapper<Staff> wrapper = new QueryWrapper<>();
        wrapper.eq("username",staff.getUsername());

        Staff staff1 = staffMapper.selectOne(wrapper);

        if(staff1!=null) {
            message = "查询成功";
            code = 201;
        }
        else {
            message = "查询失败";
            code = 400;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("staff", staff1);



        return new ResponseResult(code, message,map);

    }

    @Override
    public ResponseResult selectAllStaff() {
        int code;
        String message;
        List<Map<String, Object>> map = staffMapper.selectMaps(null);
        System.out.println(map);
        if(map.size()>0) {
            message = "查询成功";
            code = 201;
        }
        else {
            message = "查询失败";
            code = 400;
        }
        return new ResponseResult(code, message,map);
    }



    @Override
    public ResponseResult selectLike(String str) {
        int code;
        String message;
        QueryWrapper<Staff> wrapper = new QueryWrapper<>();
        // 左和右  t%
        wrapper.like("username", str)
                .or().like("title",str);

        List<Map<String, Object>> map = staffMapper.selectMaps(wrapper);

        if(map.size()>0) {
            message = "查询成功";
            code = 201;
        }
        else {
            message = "查询失败";
            code = 400;
        }
        return new ResponseResult(code, message,map);
    }


}
